# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest

import numpy as np

import paddle
import paddle.distributed as dist


class TestDenseTensorToDistAPI(unittest.TestCase):
    def setUp(self):
        self._shape = eval(os.getenv("shape"))
        self._dtype = os.getenv("dtype")
        self._seed = 2023
        self._backend = os.getenv("backend")
        self._mesh = dist.ProcessMesh([0, 1], dim_names=["x"])
        paddle.seed(self._seed)
        np.random.seed(self._seed)

    def run_test_dense_tensor_to_dist_api(self):
        if self._backend == "cpu":
            paddle.set_device("cpu")
            place = paddle.CPUPlace()
        elif self._backend == "gpu":
            place = paddle.CUDAPlace(dist.get_rank())

        dense_dist_tensor = paddle.rand([4, 10])
        dense_dist_tensor._to_dist_([dist.Replicate()], self._mesh)
        assert dense_dist_tensor.is_dist()

    def test_case(self):
        self.run_test_dense_tensor_to_dist_api()


if __name__ == "__main__":
    unittest.main()
